package com.example.meitu.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.meitu.api.common.Result;
import com.example.meitu.api.common.UserLoginContextHolder;
import com.example.meitu.api.domain.MediaType;
import com.example.meitu.api.dto.SaveUserMediaTypeForm;
import com.example.meitu.api.dto.UserMediaTypeDTO;
import com.example.meitu.api.service.MediaTypeService;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/mediaType")
public class MediaTypeController {

    @Resource
    private MediaTypeService mediaTypeService;

    @GetMapping("/list")
    public Result list(@RequestParam(value = "type", required = true) String type,
                       @RequestParam(value = "pId", required = true) Integer pId,
                       @RequestParam(value = "brandId", required = true) Integer brandId){
        Integer userId = UserLoginContextHolder.get().getUserId();
        List<MediaType> list = mediaTypeService.listByTypeAndPid(type, pId,userId,brandId);
        return Result.success(list);
    }

    @PostMapping("/saveUserMediaType")
    public Result saveUserMediaType(@RequestBody SaveUserMediaTypeForm saveUserMediaTypeForm){
        mediaTypeService.saveUserMediaType(saveUserMediaTypeForm);
        return Result.success();
    }

    @GetMapping("/getUserMediaType")
    public Result getUserMediaType(@RequestParam(value = "type", required = true) String type,
                       @RequestParam(value = "brandId", required = true) Integer brandId){
        List<UserMediaTypeDTO> list = mediaTypeService.getUserMediaType(type, brandId);
        return Result.success(list);
    }

    @GetMapping("/searchMediaType")
    public Result searchMediaType(@RequestParam(value = "keyword", required = true) String keyword){
        LambdaQueryWrapper<MediaType> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(MediaType::getDelFlag, '0');
        lambdaQueryWrapper.in(MediaType::getScope, "0");
        lambdaQueryWrapper.like(MediaType::getName, keyword);
        List<MediaType> collect = mediaTypeService.list(lambdaQueryWrapper);
        return Result.success(collect);
    }

}
